import {uploadfile,login, downloadFile} from "@utils/http"
import dayjs from "dayjs"
import { FormEvent } from "miniprogram/@types/Event"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:{} as {[key:string]:string},
    file:"",
    sexs:["女","男"],
  },
  async init(){
    const user=wx.getStorageSync("userInfo")
    if(!user){
      wx.navigateBack({
        delta:1
      })
    }
    const file=await downloadFile(user.profile)
    this.setData({
      user:{
        ...user,
        birthday:dayjs(user.birthday).format("YYYY-MM-DD")
      },
      file:file.tempFilePath,
    })
  },
  
  getimage(){
    var that=this
    wx.chooseMedia({
      count: 1,
      sizeType: ['original', 'compressed'],//图片尺寸：original:原图，compressed：压缩图
      sourceType: ['album', 'camera'],//图片来源：album：从相册选图，camera:使用相机
      mediaType: ['image'],
      camera:'back',
      success:(res)=> {
      that.setData({
        file:res.tempFiles[0].tempFilePath
      })
      }
      })
  },
  saveFormData(e:FormEvent<{key:string}>){
    const key=e.currentTarget.dataset.key
    this.setData({
      user:{
        ...this.data.user,
        [key]:e.detail.value
      }
    })
  },
  async sumbit(){
        const {sign,sex,name,birthday}=this.data.user
        const formdata={
          file:[{tempFilePath:this.data.file}],
          sign,
          birthday:dayjs(birthday).format("YYYY-MM-DD HH:mm:ss"),
          sex,
          name
        }
       await uploadfile("/user",formdata,"file","patch")
        await login()
        wx.showToast({
          title:"修改成功"
        })
        wx.navigateBack({
          delta:1
        })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.init()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})